package com.hw.kafkaThread03;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.checkerframework.checker.units.qual.K;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class KafkaConsumerBase {
    public final Properties configs;

    public KafkaConsumerBase() {
        this.configs = buildConfig();
    }

    public Properties buildConfig() {
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:8080");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "group.demo");
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
        return properties;
    }

    public void Scheduled(String[] args) {
        List<String> topics = Arrays.asList("tcp_link", "agent");
        KafkaConsumerThread kafkaConsumerThread = new KafkaConsumerThread(this.configs, topics, Runtime.getRuntime().availableProcessors());
    }

    public static class KafkaConsumerThread extends Thread {
        private KafkaConsumer kafkaConsumer;
        private ThreadPoolExecutor executor;
        private Map<TopicPartition, OffsetAndMetadata> offsets;

        public KafkaConsumerThread(Properties configs, List<String> topics, int threadNum) {
            this.kafkaConsumer = new KafkaConsumer<>(configs);
            kafkaConsumer.subscribe(topics);
            executor = new ThreadPoolExecutor(threadNum, threadNum, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1000), new ThreadPoolExecutor.CallerRunsPolicy());
        }

        @Override
        public void run() {
            try {
                while (true) {
                    ConsumerRecords<String, String> records = kafkaConsumer.poll(Duration.ofSeconds(10));
                    Iterable<ConsumerRecord<String, String>> recordIterable = records.records("tcp_link");
                    recordIterable.forEach(record -> {
                        record.partition();
                        record.value();
                    });
                    if (!records.isEmpty()) {
                        executor.submit(new MetricParser(records, offsets));
                        synchronized (offsets) {
                            if (!offsets.isEmpty()) {
                                kafkaConsumer.commitSync();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                kafkaConsumer.close();
            }
        }
    }
}
